import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Form, InputNumber } from 'antd'
import _ from 'loadsh'
import { Line, Cell, Split } from '@components/ApprovePre/Approve'
import style from './SecretaryInput.module.less'

const { Item } = Form
const layout = { labelCol: { flex: '72px' } }
export default class SecretaryInput extends Component {
  state = {
    keyTitleMap: {}
  }

  initKeyTitleMap () {
    const map = {}
    const list = _.get(this.props.instance || {}, 'currentStep.vars', [])
    list.forEach(li => {
      map[li.key] = li.title
    })
    this.setState({
      keyTitleMap: map
    })
  }

  componentDidUpdate (prev) {
    if (this.props.instance !== prev.instance) {
      this.initKeyTitleMap()
    }
  }

  componentDidMount () {
    this.initKeyTitleMap()
  }

  render () {
    const { keyTitleMap } = this.state
    return (
      <div>
        <Line>
          <Cell>
            <Item {...layout} label='发起秘书'>
              <span>{this.props.userInfo.realname}</span>
            </Item>
          </Cell>
          <Split />
          <Cell>
            <Item {...layout} name={['vars', 'delay_day']} label={keyTitleMap.delay_day} rules={[{ required: true, message: `${keyTitleMap.delay_day}不能为空` }]}>
              <DelayDay placeholder={`${keyTitleMap.delay_day}不能为空`} />
            </Item>
          </Cell>
          <Split />
        </Line>
      </div>
    )
  }
}

SecretaryInput.proptypes = {
  caseDetail: PropTypes.object,
  userInfo: PropTypes.object,
  instance: PropTypes.object,
  formRef: PropTypes.object
}

SecretaryInput.defaultProps = {
  caseDetail: {},
  userInfo: {},
  instance: {},
  formRef: {}
}

class DelayDay extends React.Component {
  render () {
    return (
      <div className={style['delay-day']}>
        <InputNumber value={this.props.value} className={style['delay-day-input']} placeholder={this.props.placeholder} onChange={d => this.props.onChange(d)} />
        <span className={style.unit}>工作日</span>
      </div>
    )
  }
}
